package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
   // DSN:Data Source Name
	dsn := "root:123456@tcp(127.0.0.1:3306)/mydoc"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		panic(err)
	}
	defer db.Close()  // 注意这行代码要写在上面err判断的下面
	err = db.Ping()//检测是否出现错误
	if err != nil {
		fmt.Printf("open %s invaild ,err'=", err)
		return
	}

	fmt.Println("connect database succsed")
	rows, _ := db.Query("select id,name,icon,description from role")
	id := ""
	name := ""
	icon := ""
	description := ""
	for rows.Next() {
		rows.Scan(&id, &name,&icon,&description);
		fmt.Println(id, name,icon,description);
	}
}